package com.box.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.box.entity.DbRoles;
import com.box.service.RolesService;
import com.box.utils.LayuiResult;




@Controller
@RequestMapping("/roles")
public class RolesController {

	
	@Autowired
	private RolesService roleService;
	
	@RequestMapping(value="/getRoleList",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult selRoles(Integer page,Integer limit){
	return 	roleService.selRoles(page, limit);
	}
	
	@RequestMapping(value="/delRole/{roleId}",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult delRoles(@PathVariable("roleId") String roleId){
		System.out.println("<<<<<<<<<<<<我进到单个删除啦>>>>>>>>>>>");
	return 	roleService.delRolesById(Long.parseLong(roleId));
	}
	
	@RequestMapping(value="/delRolesAll/{roleStr}",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public LayuiResult delRolesByAll(@PathVariable("roleStr") String roleStr){
	return 	roleService.delRolesByAll(roleStr);
	}
	
	/**
	 * 更新角色信息
	 * @param roles 角色信息
	 * @param m 权限字符串
	 */

	@RequestMapping("/updRole")
	@RequiresPermissions("sys:role:update")
	@ResponseBody
	public void updRole(DbRoles role,String m) {
		//角色信息保存
		 Integer updRole = roleService.updRole(role, m);
		 System.out.println(updRole+"................角色信息保存成功");
	}
	
	
	
	/**
	 * 添加新角色
	 * @param role
	 * @param m
	 */
	@RequestMapping("/insRole")
	@RequiresPermissions("sys:role:save")
	@ResponseBody
	public LayuiResult insRole(DbRoles role,String m) {
		DbRoles r = roleService.selRoleByRoleName(role.getRoleName());
		if(r!=null){
			return new LayuiResult (500, "角色名已存在,请重试！");
		}
		//角色信息保存
		Integer insRole = roleService.insRole(role, m);
		System.out.println(insRole+".....................添加角色成功...........");
		
		return LayuiResult.ok();
	}
	
	
	/**
	 * 角色名唯一性检查
	 * @param roleName
	 * @return
	 */
	@RequestMapping("/checkRoleName/{roleName}")
	@ResponseBody
	public LayuiResult checkRoleName(Long roleId, @PathVariable("roleName")String roleName) {
		DbRoles role = roleService.selRoleByRoleName(roleName);
		if(role==null){
			return new LayuiResult(0);
		}else if(role.getRoleId()==roleId){
			return new LayuiResult(0);
		}else{
			return new LayuiResult(500,"角色名已存在！");
		}
	}
	
	/**
	 * 页面跳转
	 * 
	 * 
	 */
	
	@RequestMapping("/roleList")
	@RequiresPermissions("sys:role:list")
	public String roleList() {
		return "roles/roleList";
	}
	
	
	/**
	 * 跳转编辑角色页面
	 * @param roleId
	 * @param roleName
	 * @param roleRemark
	 * @param req
	 * @return
	 */
	@RequestMapping("/page/editRole")
	@RequiresPermissions("sys:role:update")
	public String editRole(String roleId,String roleName,String roleRemark,Model model) {
		DbRoles role=new DbRoles();
		role.setRoleId(Long.parseLong(roleId));
		role.setRoleName(roleName);
		role.setRoleRemark(roleRemark);
		model.addAttribute("role", role);
		return "roles/editRole";
	}
	
	@RequestMapping("/page/addRole")
	@RequiresPermissions("sys:role:save")
	public String addRole() {
		return "roles/addRole";
	}
}
